package com.py.credit.service.impl;

import com.py.credit.common.ServiceException;
import com.py.credit.dao.ColumnDao;
import com.py.credit.entity.Column;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.service.ColumnService;
import com.py.framework.core.common.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ColumnServiceImpl implements ColumnService {

    @Autowired
    private ColumnDao columnDao;

    @Override
    public List<Column> getListByTableId(int tableId) {

        return columnDao.selectListByTableId(tableId);
    }

    @Override
    public List<Column> getListByTableId1(Integer tableId, Page page) {

        return columnDao.selectListByTableIdForPage(tableId, page);
    }

    @Override
    public void update(Column column) {
        columnDao.updateByPrimaryKey(column);

    }

    @Override
    public void del(List<Column> columns) {
        columnDao.delColumns(columns);

    }

    @Override
    public void insert(Column column) {

        columnDao.insert(column);

    }

    @Override
    public List<Column> getList(String metricCode, String metricName) {

        return columnDao.getList(metricCode, metricName);
    }

    @Override
    @Transactional
    public void updateColumn(List<Column> addColumns, List<Column> updateColumns, List<Column> delColumns) {
        if (!CollectionUtils.isEmpty(addColumns)) {
            for (Column addColumn : addColumns) {
                if (StringUtils.isBlank(addColumn.getMetricName())) {
                    throw new ServiceException("数据项名称不能为空");
                }
                if (StringUtils.isBlank(addColumn.getMetricCode())) {
                    throw new ServiceException("数据项编码不能为空");
                }

                if (StringUtils.isBlank(addColumn.getFieldType())) {
                    throw new ServiceException("数据项类型不能为空,数据项名称:" + addColumn.getMetricName());
                }

                if ("number".equals(addColumn.getFieldType()) && (addColumn.getFieldLength() == null
                        || addColumn.getFieldLength() > 63 || addColumn.getFieldLength() <= 0)) {
                    throw new ServiceException("数字类型长度范围1~63");
                }

                if ("varchar".equals(addColumn.getFieldType()) && (addColumn.getFieldLength() == null
                        || addColumn.getFieldLength() > 4000 || addColumn.getFieldLength() <= 0)) {
                    throw new ServiceException("字符串类型长度范围1~4000");
                }

            }

            columnDao.addColumns(addColumns);

        }

        if (!CollectionUtils.isEmpty(updateColumns)) {
            for (Column column : updateColumns) {
                if (StringUtils.isBlank(column.getMetricName())) {
                    throw new ServiceException("数据项名称不能为空");
                }
                if (StringUtils.isBlank(column.getMetricCode())) {
                    throw new ServiceException("数据项编码不能为空");
                }
                if (StringUtils.isBlank(column.getFieldType())) {
                    throw new ServiceException("数据项类型不能为空,数据项名称:" + column.getMetricName());
                }

                if ("number".equals(column.getFieldType()) && (column.getFieldLength() == null
                        || column.getFieldLength() > 63 || column.getFieldLength() <= 0)) {
                    throw new ServiceException("数字类型长度范围1~63");
                }

                if ("varchar".equals(column.getFieldType()) && (column.getFieldLength() == null
                        || column.getFieldLength() > 4000 || column.getFieldLength() <= 0)) {
                    throw new ServiceException("字符串类型长度范围1~4000");
                }
                columnDao.updateByPrimaryKey(column);
            }
        }

        if (!CollectionUtils.isEmpty(delColumns)) {
            if (!CollectionUtils.isEmpty(delColumns)) {
                columnDao.delColumns(delColumns);
            }
        }
    }

    @Override
    public void add(List<Column> columns) {
        columnDao.addColumns(columns);
    }

    @Override
    public List<String> getColumnENByTableId(int tableId) {
        return columnDao.selectColumnENByTableId(tableId);
    }

    @Override
    public List<OptionalColumn> getOptionalColumnByTableId(int tableId) {
        return columnDao.selectOptionalColumnByTableId(tableId);
    }

    @Override
    public List<HashMap<String, String>> getColumn(int tableId) {

        return columnDao.selectColumn(tableId);
    }

    @Override
    public List<Column> getColumnNodesFromComonContent(String tableId) {

        return columnDao.selectNodesFromCommonContent(tableId);
    }

    /**
     * @param tableId
     * @return
     * @Function 获取表中所有时间类型字段
     * @FileName ColumnServiceImpl.java
     * @MethodName getAllTimeMetricByTableId
     * @Date 2018年6月12日 上午10:46:27
     * @Author Min DongXv
     */
    @Override
    public List<Map<String, String>> getAllTimeMetricByTableId(Long tableId) {
        return columnDao.getAllTimeMetricByTableId(tableId);
    }

    /**
     * 根据sql获取值
     *
     * @param
     * @return
     * @author YaoS
     * @date 18:32 18/11/4
     **/
    @Override
    public String getValueBySql(String sql, String tybm) {

        StringBuffer sb = new StringBuffer();
        sb.append("select value from (");
        sb.append(sql);
        sb.append(")d where tybm=");
        sb.append("'");
        if (StringUtils.isNotBlank(tybm)) {
            sb.append(tybm);
        }
        sb.append("'");
        String mySql = sb.toString();
        mySql.replaceAll("\n", " ");
        return columnDao.getValueBySql(mySql);
    }

    @Override
    public String checkSql(String sql) {
        StringBuffer sb = new StringBuffer();
        sb.append("select value from (");
        sb.append(sql);
        sb.append(")d limit 1");
        String mySql = sb.toString();
        mySql.replaceAll("\n", " ");
        return columnDao.getValueBySql(mySql);
    }

    @Override
    public String selectTybm(String mainName){
        return columnDao.selectTybm(mainName);
    }

}
